<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
 <%@ include file="/common/query.jsp"%>
<%@ include file="/common/det.jsp"%>
<script>
		var productId = getQueryStringByDecode("productId");
		var salesHtml = "";
        $(function(){
            init();
            $.getJSON("${base}/product/getSaleAttr?id="+productId,function (res) {
				var attr = res.data;
                var html = "";
				$.each(attr,function (index,at) {
				    html += '<div class="salesAttrRow" data-id='+at.productAttrId+' data-value='+at.productAttrName+'>'
                    html += "<div>";
					html += at.productAttrName;
                    html += ":</div>";
                    html += '<div style="margin-left: 50px;">';
                    if(at.values!=null&& at.values.length>0){
                        $.each(at.values,function (index,val) {
                            html += val.productAttrValue + '<input data-id='+val.productAttrValueId+' data-value='+val.productAttrValue+' type="checkbox" checked value="checkbox" style="margin-right: 10px;" />';
                        });
					}

				    html += "</div>";
                    html += "</div>";
                });
                salesHtml = '<div id="salesAttrSelect" style="font-size: 20px;">'+html+'</div>';

				$('#salesAttrSelect').html(html);
                //<input name="checkbox" type="checkbox" value="checkbox" checked="checked" />
            });
        });
        function init(){
            var setting = {};
            var url ="";
            var p_insert = false;
            var p_del = false;
            var columns = [
                {field:'id',title:'id',hidden:true},
                {field:'productId',title:'productId',hidden:true},
                {field:'codeName',title:'SKU全称',update:false,insert:false},
                {field:'code',title:'code',hidden:true},
                {field:'fee',title:'价格',type:"number"},
                {field:'bizCode',title:'编码'}
            ];
            url='/product/sku/manager?search_EQ_productId='+productId;
            //setting.singleSelec	t = true;
            createQuery('body',columns,{pager:false,edit:true,searchBar:{hidden:true},add:false,del:false,heji:false,

                toolbar:[{
                    text : '<a  class="pure-button pure-button-primary" >批量添加SKU</a>',
                    handler : function(){
                        layer.open({
                            type: 1,
                            title: "添加",
                            shadeClose: true,
                            shade: 0.8,
                            area: ['50%', '90%'],
                            content: salesHtml,
                            btn:['确定'],
                            yes: function (index, layero) {
                                //检查是否全选中
								var data = [];
								$('.salesAttrRow').each(function () {
								    var attr = {};
									var $this = $(this);
									var values = null;
									$this.find('input:checked').each(function () {
									    if(values==null){
									        values = {};
										}
										var valueId = $(this).data("id");
                                        var valueValue = $(this).data("value");
                                        values[valueId]=valueValue;
                                    });
									if(values==null){
									    alert("请选择相应的属性");
									    return ;
									}
                                    attr.id = $this.data('id');
                                    attr.name = $this.data('value');
                                    attr.values = values;
                                    data.push(attr);
                                });

                                $.ajax({
                                    type : 'post',
                                    url : '${base}/product/sku/batchAddSku?productId='+productId,
                                    data : {
                                        json:JSON.stringify(data)
                                    },
                                    async:false,
                                    dataType : 'json',
                                    success :  function(res){
                                        if(res.code==-1){
                                            alert(res.msg);
                                        }else{
                                            window.location.reload();
                                            layer.close(index);
                                        }
                                    }
                                })

                            },
                        })

                    }
                },
                    {text : '<a  class="pure-button pure-button-primary" >批量删除</a>',
                    handler : function(){
                        BatchdelMethod(url);
                    }
                    }]

			},url);

        }

        function BatchdelMethod(url){
            var row = getGridChecked();
            $.messager.confirm('提示', '确定要删除吗?', function(r){
                if (r){
                    for(var i=0;i<row.length;i++){
                        row[i].op = "del";
                        $.ajax({
                            url:url,
                            data:row[i],
                            async:false,
                            method:"post",
                            dataType: "json",
                            success: function(res){
                                $.messager.show({
                                    title:'提示',
                                    msg:res.msg,
                                    showType:'slide',
                                    timeout:500,
                                    style:{
                                        right:'',
                                        top:document.body.scrollTop+document.documentElement.scrollTop,
                                        bottom:''
                                    }
                                });
                                search();
                            }
                        });
                    }
                }
            });
            $('.messager-window').css("top",$(document).scrollTop() + ($(window).height()-250) * 0.1);
            $('.window-shadow').hide();
        }
</script>
</head>
<body>
	  <div id="body"></div>
</body>
</html>